<?php
// $Id: login.php 436 2012-06-07 04:53:00Z tomqin@gmail.com $

defined('IN_ARESCMS') or exit('Access Denied!');

class Reg_Action extends Action_Front{

    function onIndex(){
        $this->setVar('site_desc', '用户注册');
        $this->render('user/register');
    }

    function onForget(){
        $this->render('user/forget');
    }

    function onOrder(){
        $this->setVar('site_desc', '外卖订单');
        $this->render('user/order_list');
    }

    function onDone(){
        $error = '';
        if(array_get_bool($_POST, 'dosubmit')){
            $user_name  = array_get_string($_POST, 'username');
            $user_email = array_get_string($_POST, 'txtemail');

            if($this->_check('name', $user_name, $error) && $this->_check('email', $user_email, $error)){
                if($this->_reg($_POST)){
                    $this->setVar('user_name', $user_name);
                    $this->setVar('user_email', $user_email);
                }else{
                    $error = '注册失败，请稍候再试';
                }
            }
        }else{
            header('Location: /reg');
            exit;
        }

        if($error == 'OK'){
            $this->render('user/reg_success');
        }else{
            $this->setVar('error', $error);
            $this->render('user/register');
        }
    }

    function onCheckField(){
        $type  = array_get_string($_REQUEST, 'clientid');
        $value = array_get_string($_REQUEST, $type);
        $ret   = $this->_check($type, $value, $errmsg);

        if($ret){
            exit('OK');
        }else{
            exit($errmsg);
        }
    }

    function _error($error){
        $msg   = array(
            -1  => '参数错误',
            0   => 'OK',
            11  => '用户名已存在，请更换用户名',
            12  => '用户名格式错误，请重试',
            11  => '邮件地址已存在，请更换邮件',
            22  => '邮件地址格式错误，请重试',
        );
        return isset($msg[$error]) ? $msg[$error] : '';
    }

    function _check($type, $value, &$errmsg){
        $errmsg = 'OK';
        if(strpos($type, 'name')!==false){
            if(preg_match('/^[a-zA-Z\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $value)){
                $ret = Db::getFirst("SELECT COUNT(*) FROM `biz_user` WHERE name LIKE '$value'");
                if($ret){
                    $errmsg = '用户名已存在，请更换用户名';
                    return false;
                }else{
                    return true;
                }
            }else{
                $errmsg = '用户名包含非法字符，请重试';
                return false;
            }
        }elseif(strpos($type, 'mail')!==false){
            if(is_email($value)){
                $ret = Db::getFirst("SELECT COUNT(*) FROM `biz_user` WHERE email LIKE '$value'");
                if($ret){
                    $errmsg = '用户名已存在，请更换用户名';
                    return false;
                }else{
                    return true;
                }
            }else{
                $errmsg = '用户名已存在，请更换用户名';
                return false;
            }
        }else{
            $errmsg = '参数错误';
            return false;
        }
    }

    function _reg(array $post){
        $salt   = randomstr();
        $verify = randomstr();
        $pwd    = md5($salt.md5(array_get_string($post, 'password')));
        $name   = array_get_string($post, 'username');
        $email  = array_get_string($post, 'txtemail');

        $data = array(
            'name'        => $name,
            'email'       => $email,
            'pwd'         => $pwd,
            'salt'        => $salt,
            'register_ip' => App::getClientIp(),
            'register_dt' => date('Y-m-d H:i:s'),
            'status'      => 1,
            'verify'      => $verify,
         );
        return Db::insert('biz_user', $data);
    }
}
?>